package com.guonl.controller;

//import com.github.pagehelper.PageInfo;
import com.guonl.domain.User;
import com.guonl.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private UserService userService;

    @ResponseBody
    @RequestMapping("/add")
    public int addUser(User user){
        return userService.addUser(user);
    }

    @ResponseBody
    @RequestMapping("/addAll")
    public void addUserAll(){
        for (int i = 1; i < 100; i++) {
            User user = new User();
            user.setUserName("haha"+i);
            user.setPassword("pwd"+i);
            user.setPhone("pho"+i);
            userService.addUser(user);
        }
    }

//    @ResponseBody
//    @RequestMapping("/all")
//    public Object findAllUser(PageInfo pageInfo){
//        return userService.findAllUser(pageInfo.getPageNum(),pageInfo.getPageSize());
//    }
    @ResponseBody
    @RequestMapping("/all2")
    public Object findAllUser(User user){
        return userService.selectAll(user);
    }

    @ResponseBody
    @RequestMapping("/delete")
    public Integer delete(String ids){
        String[] split = ids.split(",");
        List<Integer> ids2 = new ArrayList<>();
        for (String s : split) {
            ids2.add(Integer.parseInt(s));
        }
        System.out.println("ids = " + ids2);
        return userService.delete(ids2);
    }
    @ResponseBody
    @RequestMapping("/save")
    public Object save(User user){
        Object allUser = findAllUser(new User());
        if (user != null && user.getUserId() != null){
            userService.update(user);
        }else {
            userService.save(user);
        }
        return allUser;
    }

    @ResponseBody
    @RequestMapping("/findById")
    public Object findById(String userId){
        return userService.findById(userId);
    }
}